package com.zcl.day13_demo7;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcl.day13_demo7.adapter.FgtAdapter;
import com.zcl.day13_demo7.fragment.HomeFragment;
import com.zcl.day13_demo7.fragment.MyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private ViewPager viewpager;
    private TabLayout tablayout;
    private LinearLayout line;
    private NavigationView na;
    private DrawerLayout draw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        line = (LinearLayout) findViewById(R.id.line);
        na = (NavigationView) findViewById(R.id.na);
        draw = (DrawerLayout) findViewById(R.id.draw);
        toolbar.setTitle("首页");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, draw,  toolbar, R.string.app_name, R.string.app_name);
        draw.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        draw.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {
                int right = na.getRight();
                line.setX(right);
            }

            @Override
            public void onDrawerOpened(@NonNull View view) {

            }

            @Override
            public void onDrawerClosed(@NonNull View view) {

            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });
        na.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.item:
                        Toast.makeText(MainActivity.this,"开启消息提醒",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item2:
                        Toast.makeText(MainActivity.this,"我的音乐",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item3:
                        Toast.makeText(MainActivity.this,"我的相册",Toast.LENGTH_SHORT).show();
                        break;
                }

                return false;
            }
        });
        View headerView = na.getHeaderView(0);
        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"这是头像",Toast.LENGTH_SHORT).show();
            }
        });
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new MyFragment());
        FgtAdapter fgtAdapter = new FgtAdapter(getSupportFragmentManager(),fragments);
        viewpager.setAdapter(fgtAdapter);
        tablayout.setupWithViewPager(viewpager);
        tablayout.getTabAt(0).setText("首页");
        tablayout.getTabAt(1).setText("我的");
    }
}
